from django.shortcuts import render, get_object_or_404, redirect, reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse_lazy
from django.views import generic
from django.views.decorators.http import require_POST

from .models import Network, Station


class NetworkCreateView(generic.CreateView):
    model = Network
    fields = '__all__'
    success_url = reverse_lazy('seisnet:index')


class NetworkUpdateView(generic.UpdateView):
    model = Network
    slug_field = 'code'
    slug_url_kwarg = 'code'
    fields = '__all__'
    template_name_suffix = '_form'
    success_url = reverse_lazy('seisnet:index')


@require_POST
def network_delete(request):
    code = request.POST.get('code')
    network = get_object_or_404(Network, code=code)
    network.is_deleted = True
    network.code = "{}${}".format(network.code, network.pk)
    network.save()
    return HttpResponse('ok')


class NetworkListView(generic.ListView):
    model = Network
    template_name = 'seisnet/network_list.html'
    context_object_name = 'network_list'

    def get_queryset(self):
        return super(NetworkListView, self).get_queryset().exclude(code='XX')


class NetworkDetailView(generic.DetailView):
    model = Network
    slug_field = 'code'
    slug_url_kwarg = 'code'
    template_name = 'seisnet/network_detail.html'
    context_object_name = 'network'

    def get_object(self, queryset=None):
        code = self.kwargs['code'].upper()
        network = None

        if code:
            network = get_object_or_404(Network, code=code)

        return network

    def get_context_data(self, **kwargs):
        context = super(NetworkDetailView, self).get_context_data(**kwargs)
        station_list = self.object.station_set.all()
        context.update({
            'station_list': station_list
        })
        return context


class StationListView(generic.ListView):
    model = Station
    template_name = 'seisnet/station_list.html'
    context_object_name = 'station_list'

    def get_queryset(self):
        network = get_object_or_404(Network, code=self.kwargs['network_code'])
        return super(StationListView, self).get_queryset().filter(network=network)

    def get_context_data(self, **kwargs):
        context = super(StationListView, self).get_context_data(**kwargs)
        network_code = self.kwargs['network_code']
        network = get_object_or_404(Network, code=network_code)
        context.update({
            'network': network
        })
        return context


class StationDetailView(generic.DetailView):
    model = Station
    template_name = 'seisnet/station_detail.html'
    context_object_name = 'station'

    def get_object(self, queryset=None):

        station = None

        network_code = self.kwargs['network_code'].upper()
        code = self.kwargs['code'].upper()

        if network_code:
            network = get_object_or_404(Network, code=network_code)

            if network and code:
                station = get_object_or_404(Station, network=network, code=code)

        return station


class StationCreateView(generic.CreateView):
    model = Station
    fields = (
        'code', 'name_zh', 'name_en', 'longitude', 'latitude',
        'altitude', 'status', 'location', 'describe', 'c_date',
        'm_date', 'e_date')

    def form_valid(self, form):
        network = None

        if self.kwargs['network_code']:
            network = get_object_or_404(Network, code=self.kwargs['network_code'])
        else:
            network = get_object_or_404(Network, code='XX')

        form.instance.network = network

        return super(StationCreateView, self).form_valid(form)


class StationUpdateView(generic.UpdateView):
    model = Station
    slug_field = 'code'
    slug_url_kwarg = 'code'
    fields = '__all__'
    template_name_suffix = '_form'
